package com.itheima.web.servlet;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.service.UserSeriver;
import com.itheima.service.impl.UserSeriverImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {

    public void checkName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");

        UserSeriverImpl userSeriver = new UserSeriverImpl();

        int count = userSeriver.findByName(name);
        PrintWriter writer = response.getWriter();
        if(count == 0){
            writer.write("{\"ok\":\"名字很棒!\"}");
        }else{
            // 姓名不可用
            writer.write("{\"error\":\"姓名已存在\"}");
        }
    }

   public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0. 获取分页参数
        String currentPageStr = request.getParameter("currentPage");  // 当前页

        int currentPage = 1;  // "2"  null  "中文"
        try {
            currentPage = Integer.parseInt(currentPageStr);
        } catch (NumberFormatException e) {
            // e.printStackTrace();
        }
        int pageSize = 5; // 每页显示条数 (后期由页面传递, 临时写的假数据)

        UserSeriver us = new UserSeriverImpl();
        PageInfo<User> users = us.findAll(currentPage,pageSize);

        request.setAttribute("pb",users);

        System.out.println(users);

        request.getRequestDispatcher("/list.jsp").forward(request,response);


    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {


        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserSeriver us = new UserSeriverImpl();
        us.save(user);

        response.sendRedirect(request.getContextPath()+"/userServlet?method=findAll");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");

        UserSeriver us = new UserSeriverImpl();
        us.deleteById(id);
        //Bean
        response.sendRedirect(request.getContextPath()+"/userServlet?method=findAll");
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,map);
        UserSeriverImpl seriver = new UserSeriverImpl();
        seriver.update(user);

        response.sendRedirect(request.getContextPath()+"/userServlet?method=findAll");
    }


    public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取修改id
        String id = request.getParameter("id");

        //2. 调用service查询
        UserSeriver service = new UserSeriverImpl();
        User user = service.findById(id);

        //3. 将user添加到请求域中, 转发到update.jsp 回显
        request.setAttribute("u",user);
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }

    public void deleteAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取删除id值
        String[] ids = request.getParameterValues("id");
        //2. 调用service删除
        UserSeriver service = new UserSeriverImpl();
        service.deleteAll(ids);
        response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
    }


}
